//
//  ViewController.m
//  PDFKitTest
//
//  Created by Emo, on 2024/1/22.
//

#import "ViewController.h"
#import "PKNavigationView.h"
#import "EditPDFView.h"
#import "FileUtil.h"
#import "PKDrawingLineView.h"
#import "PKMorePopoverVC.h"

@interface ViewController () <PKNavigationViewDelegate, PKMorePopoverDelegate, UIPopoverPresentationControllerDelegate>

@property (nonatomic, strong) PKNavigationView * navigationView;

@property (nonatomic, strong) EditPDFView *editView;

@property (nonatomic, strong) PKDrawingLineView *lineView;

@property (nonatomic, assign) PKPopoverSelectType addType;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 导航栏
    [self initNavigationView];
    // PDFView
    [self initEditPDFView];
    
}

- (void)initNavigationView {
    self.navigationView = [[PKNavigationView alloc] init];
    self.navigationView.delegate = self;
    [self.view addSubview:self.navigationView];
}

- (void)initEditPDFView {
    self.editView = [[EditPDFView alloc] initWithFrame:CGRectMake(0, DON_NavBarH, DON_ScreenW, DON_ScreenH - DON_NavBarH)];
    [self.view addSubview:self.editView];
    
    // 保存新PDF的路径
    NSString *pdfPath = [FileUtil getFilePath:PDF_FILE_NAME];
    if (![FileUtil isFileExisted:PDF_FILE_NAME]) {
        // 若无新PDF,  则展示本地原始PDF
        pdfPath = [[NSBundle mainBundle] pathForResource:@"Document" ofType:@"pdf"];
    }
    self.editView.localPDFPath = pdfPath;
}



#pragma mark -- PKNavigationViewDelegate --
- (void)navigationViewDidClickEdit {
    NSLog(@"编辑");
    if (self.addType == PKPopoverSelectTypeDraw) {
        _lineView.hidden = NO;
    }
}

- (void)navigationViewDidClickPreview {
    NSLog(@"预览");
    if (self.addType == PKPopoverSelectTypeDraw) {
        _lineView.hidden = YES;
    }
}

- (void)navigationViewDidClickSave {
    if (_lineView) {
        [_lineView removeFromSuperview];
    }
    if (!_lineView.needSave) {
        NSLog(@"绘制未修改内容！");
        return;
    }
    BOOL createSuccess = [FileUtil createFileAtPath:PDF_FILE_NAME];
    NSString *path = [FileUtil getFilePath:PDF_FILE_NAME];
    if (createSuccess) {
        NSLog(@"文件路径创建成功");
    } else {
        NSLog(@"文件路径已存在");
    }
    [self.editView saveToFile:path block:^(BOOL success) {
        NSLog(@"PDF保存状态：%d", success);
        [[PKTools sharedInstance] shareFileWithTitle:@"已编辑" path:path];
    }];
}

- (void)navigationViewDidClickClean {
    [_lineView cleanAllAnnotations];
}

- (void)navigationViewDidClickMore:(UIButton *)sender {
    PKMorePopoverVC *vc = [[PKMorePopoverVC alloc] init];
    vc.popoverType = PKPopoverTypeImageTitle;
    vc.delegate = self;
    vc.modalPresentationStyle = UIModalPresentationPopover;
    vc.popoverPresentationController.sourceView = sender;
    vc.popoverPresentationController.delegate = self;
    //rect参数是以view的左上角为坐标原点（0，0）
    vc.popoverPresentationController.sourceRect = sender.bounds; //指定箭头所指区域的矩形框范围（位置和尺寸），以view的左上角为坐标原点
    vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny; //箭头方向
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark -- PKMorePopoverDelegate --
- (void)popoverVC:(PKMorePopoverVC *)vc didSelectCell:(PKPopoverSelectType)type {
    self.addType = type;
    if (type == PKPopoverSelectTypeText) {
        _lineView.hidden = YES;
        
    } else if (type == PKPopoverSelectTypeDraw) {
        if (!_lineView) {
            [self.editView addSubview:self.lineView];
        }
        _lineView.hidden = NO;
    }
}

- (PKDrawingLineView *)lineView {
    if (!_lineView) {
        _lineView = [[PKDrawingLineView alloc] initWithFrame:self.editView.bounds];
        self.lineView.pdfView = self.editView.pdfView;
    }
    return _lineView;
}










#pragma mark - UIPopoverPresentationControllerDelegate

/// 在默认的情况下,UIPopoverPresentationController 会根据是否是 iphone 和 ipad 来选择弹出的样式,如果当前的设备是 iphone ,那么系统会选择 modal 样式,并弹出到全屏.如果我们需要改变这个默认的行为,则需要实现代理,在代理 - adaptivePresentationStyleForPresentationController: 这个方法中返回一个 UIModalPresentationNone 样式
/// @param controller controller
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

- (BOOL)presentationControllerShouldDismiss:(UIPresentationController *)presentationController {
    return YES;
}

@end
